import { Reducer } from 'redux';
import { Effect } from 'dva';
import { message } from 'antd';
import _ from 'lodash';
import {
  fetchListAsync,
  createItemAsync,
  updateItemAsync,
  deleteItemAsync,
  fetchFilterOptsAsync,
  enableItemAsync,
  fetchRolesAsync,
  IRoleItem,
} from '@/services/manageUser';
import generateCommonModel, {
  CommonModelState,
  CommonModelEffects,
  CommonModelReducers,
} from '@/utils/redux';

export interface ManageUserModelState extends CommonModelState {
  roleOpts?: Array<IRoleItem>;
}

export interface ManageUserEffects extends CommonModelEffects {
  enableItem?: Effect;
  queryRoleOpts?: Effect;
}

export interface ManageUserReducers extends CommonModelReducers<ManageUserModelState> {
  setRoleOpts?: Reducer<ManageUserModelState>;
}

export interface ManageUserModelType {
  namespace: string;
  state: ManageUserModelState;
  effects: ManageUserEffects;
  reducers: ManageUserReducers;
}

const CommonModal: Partial<ManageUserModelType> = generateCommonModel('manageUser', {
  fetchListApi: fetchListAsync,
  createItemApi: createItemAsync,
  updateItemApi: updateItemAsync,
  deleteItemApi: deleteItemAsync,
  fetchFilterOptsApi: fetchFilterOptsAsync,
});

const ExtendModel: Partial<ManageUserModelType> = {
  effects: {
    *queryTableList({ payload }, { call, put }) {
      const res = yield call(fetchListAsync, payload);

      if ('0' === res.code) {
        let { records = [], total = 0 } = res.data;

        const newData = records.map(item => {
          const { sysUserAdmin = {}, sysRoles = [], sysOrganization = {} } = item;
          const roleNames: Array<string> = [];
          const roleIds: Array<number> = [];

          sysRoles.map(item => {
            roleNames.push(item.roleName);
            roleIds.push(item.id);
          });

          return {
            id: sysUserAdmin.id,
            name: sysUserAdmin.name,
            phone: sysUserAdmin.phone,
            userCode: sysUserAdmin.userCode,
            status: sysUserAdmin.status,
            roles: roleNames,
            roleIds: roleIds,
            orgId: sysOrganization.id,
            organization: sysOrganization.name,
          };
        });

        yield put({
          type: 'saveTableList',
          payload: newData,
        });
        yield put({
          type: 'saveTotal',
          payload: total,
        });
      }
    },
    *enableItem({ payload }, { call, put }) {
      const res = yield call(enableItemAsync, payload);

      if ('0' === res.code) {
        message.success('设置成功');
        return res;
      }
    },
    *queryRoleOpts({ payload }, { call, put }) {
      const res = yield call(fetchRolesAsync, payload);

      if ('0' === res.code) {
        yield put({
          type: 'setRoleOpts',
          payload: res.data,
        });
        return res;
      }
    },
  },
  reducers: {
    setRoleOpts(state, { payload }) {
      return {
        ...state,
        roleOpts: payload,
      };
    },
  },
};

const ManageUserDataModel = _.merge(CommonModal, ExtendModel);

export default ManageUserDataModel;
